import { Opt } from '__com/Opt.js'
import ruleOpt from '@/option/rule.js'
import organApi from '@/biz/organization'
import dt from '@dt/dt'

export class editOpt extends Opt {
  constructor(vm) {
    super(vm)
  }
  expandFun(data) {
    data.forEach((item) => {
      if (item.level == 1) {
        item.disabled = true
      } else if (!item.haveAuth) {
        item.disabled = true
      }
      if (item.children && item.children.length) {
        this.expandFun(item.children)
      }
    })
  }
  create(vm) {
    return [
      {
        title: '所属组织',
        key: 'orgId',
        treeSelect: {
          data: []
        },
        load: (o) => {
          organApi.getOrganization().then((res) => {
            this.expandFun(res)
            if (this.editInfo.divisionId) {
              o.treeSelect.data = res.filter((e) => {
                return e.id == this.editInfo.divisionId
              })
            } else {
              o.treeSelect.data = res
            }
          })
        },
        change: (val) => {
          dt.tool.flattenArr(val.option.treeSelect.data).map((item) => {
            if (item.id == val.value) {
              val.info.orgLevel = item.level
            }
          })
          this.changeOrgIdFun(val)
        },
        rules: [ruleOpt.required('所属组织不能为空', 'number')]
      },
      {
        title: '项目名称',
        key: 'projectName',
        input: {
          maxlength: 20
        },
        rules: [ruleOpt.required('项目名称不能为空')]
      }
    ]
  }
}
